package com.duowei.kitchenshow.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.android.volley.VolleyError;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.duowei.kitchenshow.R;
import com.duowei.kitchenshow.adapter.SpacesItemDecoration;
import com.duowei.kitchenshow.bean.Dmjyxmsslb;
import com.duowei.kitchenshow.bean.Jyxmsz;
import com.duowei.kitchenshow.event.CancelSaleOut;
import com.duowei.kitchenshow.event.SaleOut;
import com.duowei.kitchenshow.event.StartAnim;
import com.duowei.kitchenshow.event.StopAnim;
import com.duowei.kitchenshow.httputils.DownHTTP;
import com.duowei.kitchenshow.httputils.VolleyResultListener;
import com.duowei.kitchenshow.service.Net;
import com.duowei.kitchenshow.utils.KeyboardUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.litepal.crud.DataSupport;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class UnSellOutFragment extends Fragment implements BaseQuickAdapter.OnRecyclerViewItemClickListener, View.OnClickListener, TextWatcher {
    public UnSellOutFragment() {
        // Required empty public constructor
    }
    private final String sqlDMJYXMSSLB="select * from dmjyxmsslb where isnull(sfty,'0')<>'1' and lbbm<>'RICH' and isnull(sfxs,'0')<>'1'" +
            "and lbbm in(select lbbm from jyxmsz where isnull(sfqx,'0')<>'1' and isnull(gq,'0')<>'1') order by xl|";

    private List<Dmjyxmsslb> mDmjyxmsslbList=new ArrayList<>();
    private List<Jyxmsz>mJyxmszArrayList=new ArrayList<>();

    private TopAdapter mTopAdapter;
    private BottomAdapter mBottomAdapter;
    private String mLbbm;
    private EditText mEtInput;
    private KeyboardUtil mKeyboardUtil;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_un_sell_out, container, false);
        mKeyboardUtil = KeyboardUtil.instance();

        RecyclerView rvTop = inflate.findViewById(R.id.rv_top);
        rvTop.setLayoutManager(new GridLayoutManager(getActivity(),6));
        rvTop.addItemDecoration(new SpacesItemDecoration(5));
        mTopAdapter = new TopAdapter(mDmjyxmsslbList);
        rvTop.setAdapter(mTopAdapter);
        mTopAdapter.setOnRecyclerViewItemClickListener(this);

        RecyclerView rvBottom = inflate.findViewById(R.id.rv_bottom);
        rvBottom.setLayoutManager(new GridLayoutManager(getActivity(),6));
        rvBottom.addItemDecoration(new SpacesItemDecoration(5));
        mBottomAdapter = new BottomAdapter(mJyxmszArrayList);
        rvBottom.setAdapter(mBottomAdapter);
        mBottomAdapter.setOnRecyclerViewItemClickListener(this);

        mEtInput = inflate.findViewById(R.id.et_input);
        mEtInput.setFocusable(false);
        mEtInput.setOnClickListener(this);
        mEtInput.addTextChangedListener(this);

        Http_Dmjyxmsslb();
        return inflate;
    }
    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void cancelSaleOut(CancelSaleOut event){
        setJyxmsz(mLbbm);
    }

    private void Http_Dmjyxmsslb() {
        DownHTTP.postVolley6(Net.url, sqlDMJYXMSSLB, new VolleyResultListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Type type = new TypeToken<List<Dmjyxmsslb>>() {
                }.getType();
                mDmjyxmsslbList = gson.fromJson(response, type);
                mDmjyxmsslbList.get(0).setSelect(true);
                setJyxmsz(mDmjyxmsslbList.get(0).getLBBM());

                mTopAdapter.setNewData(mDmjyxmsslbList);
            }
        });
    }

    private void setJyxmsz(String lbbm){
        mJyxmszArrayList= DataSupport.where("lbbm=? and gq!='1'",lbbm).find(Jyxmsz.class);
        mBottomAdapter.setNewData(mJyxmszArrayList);
    }

    @Override
    public void onItemClick(View view,int i) {
        if(view.getId()==R.id.ll_top){
            mLbbm = mDmjyxmsslbList.get(i).getLBBM();
            for(int j=0;j<mDmjyxmsslbList.size();j++){
                if(mDmjyxmsslbList.get(j).isSelect()){
                    mDmjyxmsslbList.get(j).setSelect(false);
                }
            }
            mDmjyxmsslbList.get(i).setSelect(true);
            mTopAdapter.setNewData(mDmjyxmsslbList);

            setJyxmsz(mLbbm);
        }else if(view.getId()==R.id.ll_bottom){
            EventBus.getDefault().post(new StartAnim());
            final Jyxmsz jyxmsz = mJyxmszArrayList.get(i);
            String updateJyxmsz="update jyxmsz set gq='1' where xmbh='"+ jyxmsz.getXmbh()+"'|";
            String updateTbrq = "update tbsj set tbrq=getdate() where tablename='jyxmsz'|";
            DownHTTP.postVolley7(Net.url, updateJyxmsz+updateTbrq, new VolleyResultListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    EventBus.getDefault().post(new StopAnim());
                }
                @Override
                public void onResponse(String response) {
                    if(response.contains("richado")){
                        EventBus.getDefault().post(new SaleOut(jyxmsz));
                        mJyxmszArrayList.remove(jyxmsz);
                        mBottomAdapter.setNewData(mJyxmszArrayList);
                    }
                }
            });
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        String s = charSequence.toString().toUpperCase();
        if(!TextUtils.isEmpty(s)){
            mJyxmszArrayList = DataSupport.where("py like ? or xmbh=?", "%" + s + "%",s).find(Jyxmsz.class);
            mBottomAdapter.setNewData(mJyxmszArrayList);
        }
    }
    @Override
    public void afterTextChanged(Editable editable) {
    }

    @Override
    public void onClick(View view) {
        mKeyboardUtil.initKeyboard(getActivity(),getActivity(),mEtInput);
        mKeyboardUtil.showKeyboard();
    }

    class TopAdapter extends BaseQuickAdapter<Dmjyxmsslb> {
        public TopAdapter(List<Dmjyxmsslb> data) {
            super(R.layout.foodkind_item,data);
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, Dmjyxmsslb dmjyxmsslb) {
            baseViewHolder.setText(R.id.ctv_top,dmjyxmsslb.getLBMC());
            if(dmjyxmsslb.isSelect()){
                baseViewHolder.setBackgroundRes(R.id.ctv_top,R.drawable.shape_blue_click);
            }else{
                baseViewHolder.setBackgroundRes(R.id.ctv_top,R.drawable.shape_blue_normal);
            }
        }
    }

    class BottomAdapter extends BaseQuickAdapter<Jyxmsz>{
        public BottomAdapter(List<Jyxmsz> data) {
            super(R.layout.food_item,data);
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, Jyxmsz jyxmsz) {
            baseViewHolder.setText(R.id.tv_food,jyxmsz.getXmmc());
        }
    }
}
